<?php

namespace app\common\model;

use think\Env;
use think\Model;

/**
 * 模型
 */
class Member extends Model
{
    // 表名
    protected $name = 'member';
    protected $deleteTime = false;
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'update_time';

    const MEMBER_TYPE_CLIENT = 0;//车主端
    const MEMBER_TYPE_SHOP= 1;//商家端

    public function getMemberSexAttr($value)
    {
        if ($value == 1){
            return '男';
        }elseif ($value == 2){
            return '女';
        }else{
            return '';
        }
    }

    public function setMemberAvatarAttr($value)
    {
        if (!empty($value)){
            $arr = parse_url($value);
            return $arr['path'];
        }
        return '';
    }

    public function getMemberAvatarAttr($value) {
        return $value ? Env::get('app.app_host') . $value : Env::get('app.app_host')."/uploads/head/default.png";
    }

    public function getMemberMobileAttr($value) {
        return $value ? $value : '';
    }

    public function setBirthdayAttr($value)
    {
        return strtotime($value);
    }

    public static function addMember($data)
    {
        $member_id = self::insertGetId([
            'member_avatar'=>$data['avatarUrl'],
            'member_name'=>$data['nickName'],
            'member_sex'=>$data['gender'],
            'member_province'=>$data['province'],
            'member_city'=>$data['city'],
            'mini_openid'=>$data['openId'],
            'logintime'=>time(),
            'login_ip'=>getIPaddress()
        ]);
        return $member_id;
    }

    public static function getMember($id)
    {
        return self::get($id);
    }

    public static function getMemberForLock($id)
    {
        return self::where('id', $id)->lock(true)->find();
    }

    /**
     * 获取会员折扣
     * @param $id
     * @return float|mixed|string
     */
    public static function getMemberDiscount($id)
    {
        return self::where('id', $id)->value('discount') ?? 1;
    }



}
